//
//  LDY_CycleViewController.m
//  ProjectOfBStage
//
//  Created by dlios on 15/11/13.
//  Copyright © 2015年 cuixin. All rights reserved.
//

#import "LDY_CycleViewController.h"
#import "Network.h"
#import "LDY_ArticleTableViewCell.h"

@interface LDY_CycleViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSString *article;

@property (nonatomic, retain) UITableView *articleTableView;

@property (nonatomic, retain) NSMutableArray *dataArr;

@property (nonatomic, retain) NSMutableArray *mainArr;

@property (nonatomic, retain) NSDictionary *dic;

@property (nonatomic, retain) LDY_ArticleModel *model;

@end

@implementation LDY_CycleViewController

- (void)dealloc
{
    [_url release];
    [_article release];
    [_articleTableView release];
    [_dataArr release];
    [_mainArr release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.article = [NSString string];
        self.dataArr = [NSMutableArray array];
        self.mainArr = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self dataHandle];
    
    [self createTableView];
}

- (void)dataHandle
{
    
    NSString *url = self.url;
    NSArray *arr = [url componentsSeparatedByString:@"/"];
    NSString *urlStr = [arr lastObject];
    
    NSString *body = [NSString stringWithFormat:@"auth=&client=1&contentid=%@&deviceid=7F95D47F-BF62-4528-B18E-EE921FFC5323&version=3.0.6", urlStr];
    
    [Network networkPOSTRequestWithURL:@"http://api2.pianke.me/article/info" body:body result:^(id result) {
        
        NSLog(@"*****%@", result);
        NSDictionary *dic = result;
        
        NSDictionary *dic2 = [dic objectForKey:@"data"];
        
        self.model = [LDY_ArticleModel modelWithDictionary:dic2];
        
//        self.article = model.html;
        
//        model.html = [[dic objectForKey:@"data"] objectForKey:@"html"];
        
//        [self.mainArr addObject:model];
        
    
//        if (self.mainArr.count > 0) {
        
            [self.articleTableView reloadData];
            
//        }
    
    }];
}


- (void)createTableView
{
    self.articleTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 60, KWIDTH * 375, KHEIGHT * 667) style:UITableViewStyleGrouped];
    [self.view addSubview:self.articleTableView];
    
    self.articleTableView.delegate = self;
    self.articleTableView.dataSource =  self;
    
    
    [self.articleTableView release];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
//    if (self.mainArr.count > 0) {
        return 1;
//    }else{
//        return 0;
//    }
   
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LDY_ArticleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LDY_ArticleTableViewCell"];
    if (!cell) {
        cell = [[LDY_ArticleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LDY_ArticleTableViewCell"];
    }
//    if (self.mainArr > 0) {
//        cell.model = [self.mainArr objectAtIndex:indexPath.row];
        
    cell.model = self.model;
    
//    }
    
    return cell;
    
}

#warning cell自适应高度的第二步: 返回每行cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    LDY_ArticleModel *model = [[LDY_ArticleModel alloc] init];
    
    
    
    CGFloat height = [LDY_ArticleTableViewCell heightOfLabel:model.html andWith:self.view.frame.size.width];
    
    return height;
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
